package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.service.PaymentService;

import com.guigu.springcloud.entities.CommonResult;
import com.guigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Date 2021/7/23 9:24
 * @Created by wangle
 * @Description
 */
@Slf4j
@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private  String serverPort;
    @PostMapping("/payment/creat")
    public CommonResult create(@RequestBody Payment payment) {

        int result = paymentService.creat(payment);
        log.info("插入成功{}", result);

        if (result > 0) {
            return new CommonResult(200, "sucess"+serverPort, result);

        } else {
            return new CommonResult(200, "插入数据库失败"+serverPort, null);

        }
    }


    @GetMapping("/payment/get/{id}")
    public CommonResult getpayment(@PathVariable("id") Long id) {

        Payment result = paymentService.getpaymentbyid(id);
        log.info("插入成功{}", result);

        if (result != null) {
            return new CommonResult(200, "查询成功"+serverPort, result);

        } else {
            return new CommonResult(200, "查询数据库失败，没有对应的记录！"+serverPort, null);

        }

    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB()
    {
        return serverPort;
    }
    
    @GetMapping("/payment/feign/timeout")
    public  String PaymentFeignTimeout(){
        System.out.println("*****paymentFeignTimeOut from port: "+serverPort);
        //暂停几秒钟线程
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        return serverPort;
        
    }



        @GetMapping("/payment/zipkin")
        public String paymentZipkin()
        {
            return "hi ,i'am paymentzipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
        }



}
